package com.sskj.mine;


import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toolbar;

import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CoinUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.mine.bean.InviteInfo;
import com.sskj.mine.invite.InviteActivity;
import com.sskj.mine.presenter.MineInviteActivityPresenter;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MineInviteActivity extends BaseActivity<MineInviteActivityPresenter> {

    @BindView(R2.id.qr_code)
    ImageView qrCode;
    @BindView(R2.id.invite_code)
    TextView inviteCode;
    @BindView(R2.id.copy_code)
    TextView copyCode;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_invite;
    }

    @Override
    public MineInviteActivityPresenter getPresenter() {
        return new MineInviteActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle(App.INSTANCE.getString(R.string.mine_inviteActivity1_j));
        ClickUtil.click(copyCode, () -> {
            if (TextUtils.isEmpty(inviteCode.getText().toString().trim())) {
                return;
            }
            CopyUtil.copy(inviteCode.getText().toString().trim());
        });
        qrCode.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new RxPermissions(MineInviteActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe(agree -> {
                            if (agree) {
                                if (ImgUtil.saveImageToGallery(MineInviteActivity.this, qrCode)==null) {
                                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineInviteActivity2_j));
                                } else {
                                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineInviteActivity3_j));
                                }
                            } else {
                                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_mineInviteActivity4_j));
                            }
                        });

                return true;
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getShareInfo();
    }

    public void updateUi(InviteInfo inviteInfo) {
        inviteCode.setText(inviteInfo.getUrl());
        ImageUtil.setImage(inviteInfo.getQrc(), qrCode);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, MineInviteActivity.class);
        context.startActivity(intent);
    }
}
